import request from '@/utils/request'

/**
 * 创建以太坊账户
 * @returns {Promise<Object>} Promise
 * @property {string} data.address 新创建账户的以太坊地址
 * @property {string} data.privateKey 新创建账户的私钥
 * @author xiaoyouxin
 */
export function createAccountApi() {
  return request({
    url: '/web3j/account/create',
    method: 'get'
  })
}

/**
 * 获取以太坊账户余额
 * @param {string} address 地址
 * @returns {Promise<Object>} Promise
 * @property {number} data 余额
 * @author xiaoyouxin
 */
export function getBalanceApi(address) {
  return request({
    url: `/web3j/account/balance/${address}`,
    method: 'get'
  })
}

/**
 * 获取一个随机主账户
 * @returns {Promise<Object>} Promise
 * @property {string} address 随机主账户的以太坊地址
 * @property {string} privateKey 随机主账户的私钥
 * @author xiaoyouxin
 */
export function getRandomAccountApi() {
  return request({
    url: '/web3j/account/random',
    method: 'get'
  })
}

/**
 * 转账操作
 * 此函数会向后端发送请求，执行以太坊的转账操作
 * @param {Object} data 转账参数
 * @param {string} data.senderPrivateKey 发送者的以太坊私钥
 * @param {string} data.receiverAddress 接收者的以太坊地址
 * @param {number} data.amount 转账金额
 * @returns {Promise<Object>} Promise
 * @author xiaoyouxin
 */
export function transferApi(data) {
  return request({
    url: '/web3j/transaction/transfer',
    method: 'post',
    params: data
  })
}
